package cuiyt.juc.pool;

import java.util.concurrent.*;

/**
 * @author cyt
 * @create 2020-05-01 21:32
 */

// 创建线程池
public class demo02 {
    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,
                2,
                3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(3), Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        try {
            for (int i = 1; i <= 2; i++) {
                // 线程池中， 使用 execute（）来创建线程
                executor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // shutdown ()来关闭线程池
            executor.shutdown();
        }
    }
}
